package com.jmt.goods.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jmt.goods.dao.GoodsDao;
import com.jmt.goods.po.Goods;
import com.jmt.system.db.DbConn;

public class GoodsDaoImpl implements GoodsDao {
	private static Connection conn ;
	
	@Override
	public boolean addGoods(Goods goods) {
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("INSERT INTO T_Goods VALUES(SEQ_T_GOODS.nextVal,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			
			pstmt.setString(1, goods.getGoodsName());
			pstmt.setString(2, goods.getGoodsIntroduce());
			pstmt.setFloat(3, goods.getGoodsNormalPrice());
			pstmt.setFloat(4, goods.getGoodsMemberPrice());
			pstmt.setFloat(5, goods.getGoodsRebate());
			pstmt.setInt(6, goods.getTypeId());
			pstmt.setInt(7, goods.getGoodsNumber());
			pstmt.setInt(8, goods.getGoodsGrade());
			pstmt.setBoolean(9, goods.isSale());
			pstmt.setBoolean(10, goods.isValid());
			pstmt.setString(11, goods.getGoodsPicture());
			pstmt.setInt(12, goods.getCreaterId());
			pstmt.setDate(13, new Date(goods.getCreateDate().getTime()));
			pstmt.setInt(14, goods.getUpdaterId());
			pstmt.setDate(15, new Date(goods.getUpdateDate().getTime()));
			
			int result = pstmt.executeUpdate();
			if (result > 0) {
				return true;
			} else {
				return false;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean deleteGoods(int goodsId) {
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("DELETE FROM T_Goods WHERE GoodsID = ?");
			pstmt.setInt(1, goodsId);
			int result = pstmt.executeUpdate();
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean updateGoods(int goodsId, String data) {
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("UPDATE T_Goods SET "+data+" WHERE GoodsID = ?");
			pstmt.setInt(1, goodsId);
			int result = pstmt.executeUpdate();
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<Goods> getAllGoods() {
		List<Goods> goodsList = new ArrayList<Goods>();
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM T_Goods");
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()){
				Goods goods = new Goods();
				goods.setGoodsId(rs.getInt(1));
				goods.setGoodsName(rs.getString(2));
				goods.setGoodsIntroduce(rs.getString(3));
				goods.setGoodsNormalPrice(rs.getFloat(4));
				goods.setGoodsMemberPrice(rs.getFloat(5));
				goods.setGoodsRebate(rs.getFloat(6));
				goods.setTypeId(rs.getInt(7));
				goods.setGoodsNumber(rs.getInt(8));
				goods.setGoodsGrade(rs.getInt(9));
				goods.setSale(rs.getBoolean(10));
				goods.setValid(rs.getBoolean(11));
				goods.setGoodsPicture(rs.getString(12));
				goods.setCreaterId(rs.getInt(13));
				goods.setCreateDate(rs.getDate(14));
				goods.setUpdaterId(rs.getInt(15));
				goods.setUpdateDate(rs.getDate(16));
				goodsList.add(goods);
			}
			return goodsList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Goods getGoodsById(int goodsId) {
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM T_Goods WHERE GoodsID = ?");
			pstmt.setInt(1, goodsId);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				Goods goods = new Goods();
				goods.setGoodsId(rs.getInt(1));
				goods.setGoodsName(rs.getString(2));
				goods.setGoodsIntroduce(rs.getString(3));
				goods.setGoodsNormalPrice(rs.getFloat(4));
				goods.setGoodsMemberPrice(rs.getFloat(5));
				goods.setGoodsRebate(rs.getFloat(6));
				goods.setTypeId(rs.getInt(7));
				goods.setGoodsNumber(rs.getInt(8));
				goods.setGoodsGrade(rs.getInt(9));
				goods.setSale(rs.getBoolean(10));
				goods.setValid(rs.getBoolean(11));
				goods.setGoodsPicture(rs.getString(12));
				goods.setCreaterId(rs.getInt(13));
				goods.setCreateDate(rs.getDate(14));
				goods.setUpdaterId(rs.getInt(15));
				goods.setUpdateDate(rs.getDate(16));
				return goods;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Goods> getGoodsByCondition(String condition) {
		List<Goods> goodsList = new ArrayList<Goods>();
		try {
			conn = DbConn.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM T_Goods WHERE " + condition);
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()){
				Goods goods = new Goods();
				goods.setGoodsId(rs.getInt(1));
				goods.setGoodsName(rs.getString(2));
				goods.setGoodsIntroduce(rs.getString(3));
				goods.setGoodsNormalPrice(rs.getFloat(4));
				goods.setGoodsMemberPrice(rs.getFloat(5));
				goods.setGoodsRebate(rs.getFloat(6));
				goods.setTypeId(rs.getInt(7));
				goods.setGoodsNumber(rs.getInt(8));
				goods.setGoodsGrade(rs.getInt(9));
				goods.setSale(rs.getBoolean(10));
				goods.setValid(rs.getBoolean(11));
				goods.setGoodsPicture(rs.getString(12));
				goods.setCreaterId(rs.getInt(13));
				goods.setCreateDate(rs.getDate(14));
				goods.setUpdaterId(rs.getInt(15));
				goods.setUpdateDate(rs.getDate(16));
				goodsList.add(goods);
			}
			return goodsList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	
}
